/**  
 * @Title: JobService.java
 * @Description: TODO(描述)
 * @author liuyi
 * @date 2023-06-26 08:30:04 
 */  
package com.fsxgt.datagrid.console.service.impl;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Map;
import java.util.UUID;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.fsxgt.datagrid.console.bo.FlowExecLogBO;
import com.fsxgt.datagrid.console.bo.JobInfoBO;
import com.fsxgt.datagrid.console.dao.FlowExecLogMapper;
import com.fsxgt.datagrid.console.exception.RunningException;
import com.fsxgt.datagrid.core.component.HttpComponent;
import com.fsxgt.datagrid.core.utils.ReturnT;

import lombok.extern.slf4j.Slf4j;

/**  
 * @ClassName: JobService
 * @Description: TODO(描述)
 * @author liuyi
 * @date 2023-06-26 08:30:04 
*/
@Service
@Slf4j
public class TaskService {
	
	@Autowired
    private HttpComponent httpComponent;
	@Autowired
    private ServerService serverService;
	@Autowired
    private FlowExecLogMapper flowExecLogMapper;
	@Autowired
    private ExecutorService executorService;
	
	
	
	public ReturnT stop(String execId,String server) {
		

		return executorService.stop(execId, server);
		
	}
	

	public boolean startTask(JobInfoBO jobInfoBO,Date triggerTime) throws RunningException {
		


		String execId = UUID.randomUUID().toString();

		String server = serverService.getServer();
		
		FlowExecLogBO logBO = new FlowExecLogBO();
		logBO.setExecId(execId);
		logBO.setTriggerTime(triggerTime);
		logBO.setServer(server);
		logBO.setJobId(jobInfoBO.getId());
		
		try {
			
			if(StringUtils.isEmpty(jobInfoBO.getVersion())) {
				throw new RunningException("任务"+jobInfoBO.getId()+"获取不到version");
			}
			
			ReturnT<Map> result = executorService.start(jobInfoBO.getObjId(),jobInfoBO.getObjType(),server,execId,triggerTime,jobInfoBO.getVersion(),jobInfoBO.getThreads(),jobInfoBO.getExecutorTimeout(),jobInfoBO.getExecutorFailRetryCount(),new ExecutorService.Callback() {
				@Override
				public void handle(String overTime, String startTime, String status, String msg, String logPath, String execId) throws Exception {
					// TODO Auto-generated method stub
					// 如果成功，并且有子任务
					SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");

					FlowExecLogBO logBO = new FlowExecLogBO();

					logBO.setExecId(execId);
					logBO.setOverTime(format.parse(overTime));
					logBO.setStartTime(format.parse(startTime));
					logBO.setStatus(status);
					logBO.setMsg(msg);
					logBO.setLogPath(logPath);

					flowExecLogMapper.updateFlowExecLog(logBO);
					
					
					if (status.equals("2")&&!jobInfoBO.getSubJobMap().isEmpty()) {
						for(JobInfoBO job:jobInfoBO.getSubJobMap().values()) {
							try {
								startTask(job,new Date());
							}catch(Exception e) {
								log.error("",e);
							}
							
						}
					}
				}
			});
			if (result.getCode().equals("200")) {
				logBO.setStatus("1");
//				logBO.setStartTime(format.parse(result.getData().get("startTime").toString()));
			} else {
				logBO.setStatus("3");
				logBO.setMsg(result.getMsg());
			}
		} catch (RunningException e) {
			throw e;
		}catch (Exception e) {
			jobInfoBO.setErrorStatus(1);
			logBO.setStatus("3");
			logBO.setMsg(e.getMessage());
			log.error("",e);
		}
		
		flowExecLogMapper.insertFlowExecLog(logBO);
		
		return logBO.getStatus().equals("1");
		
	
	}

	/**  
	 * @Title: getLog
	 * @Description: TODO(描述)
	 * @param execId
	 * @param server
	 * @return
	 * @author liuyi
	 * @date 2023-06-30 11:02:00 
	 */  
	public ReturnT getLog(String execId, String server, String triggerTime) {
		// TODO Auto-generated method stub
		
		return executorService.getLog(execId, server, triggerTime);
		

	}
	
	
	


	

}
